﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Xml;
using System.Drawing;
using System.Configuration;
using System.Text;
namespace ZX.WebApp.Message
{
    public partial class fileupload : System.Web.UI.Page
    {
        public static readonly string fileurl = ConfigurationManager.AppSettings["filepath"].ToString();
        public static readonly string picurl = ConfigurationManager.AppSettings["picurl"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //if (!Directory.Exists(Server.MapPath(fileurl) + Request.QueryString["title"]))
                //{
                //    Directory.CreateDirectory(Server.MapPath(fileurl) + Request.QueryString["title"]);
                    
                //}
                //else
                //{
                //    Response.Write("<script>alert('该彩信已存在，请确定是否需要覆盖，如不覆盖，请返回更改彩信名再上传文件')</script> ");
                //}
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string[] ts = Request.QueryString["types"].Split('_');
            if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
            {
                Response.Write("<script>alert('请您选择文件')</script> ");
                return;//当无文件时,返回
            }
            string ft = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
            if (FileUpload1.PostedFile.ContentLength > 1024 * 30)
            {
                Response.Write("<script>alert('文件大于30K，请处理后再上传')</script> ");
                return;//当无文件时,返回
            }
            if (checktype(ts, ft))
            {
                string filename = Server.MapPath(fileurl + Request.QueryString["title"]);
                FileUpload1.SaveAs(filename + "/" + ts[2] + ft);
                Label1.Text = "上传成功！";
                Label1.ForeColor = Color.Red;
                if (ts[1] == "txt")
                {
                    //如何在textbox里显示txt文件的内容   
                    string path = filename + "/" + ts[2] + ft;//读取文件txt   
                    StringBuilder b = new StringBuilder();
                    using (FileStream fs = new FileStream(path, FileMode.Open))
                    {
                        using (StreamReader sr = new StreamReader(fs,Encoding.GetEncoding("GB2312")))
                        {
                            while (!sr.EndOfStream)
                            {
                                string sLine = sr.ReadLine();
                                if (sLine.Length < 1)
                                {
                                    continue;
                                }
                                b.Append(sLine);
                            }
                        }
                    }
                    hdpath.Value = b.ToString();
                }
                else if (ts[1] == "aud")
                {
                    hdpath.Value =ts[2] + ft;
                }
                else
                {
                    hdpath.Value = picurl + Request.QueryString["title"] + "/" + ts[2] + ft;
                }
            }
            else
            {
                Response.Write("<script>alert('请注意上传的文件格式是否符合要求')</script> ");
                return;//当无文件时,返回
            }

        }

        //判断上传文件类型是否匹配
        public bool checktype(string[] ts, string st2)
        {
            string[] ps = {".jpg",".gif"};
            string[] au={".mid",".amr"};
            bool bl = false;
            if (ts[1] == "pic")
            {
                for (int i = 0; i < ps.Length; i++)
                {
                    if (st2 == ps[i])
                    {
                        bl = true;
                        File.Delete(ts[2] + ".jpg");
                        File.Delete(ts[2] + ".gif");
                        break;
                    }
                }
            }
            else if (ts[1] == "aud")
            {
                for (int i = 0; i < au.Length; i++)
                {
                    if (st2 == au[i])
                    {
                        bl = true;
                        File.Delete(ts[2] + ".mid");
                        File.Delete(ts[2] + ".amr");
                        break;
                    }
                }
            }
            else if (ts[1] == "txt" && st2==".txt")
            {
                bl = true;
                File.Delete(ts[2] + ".txt");
            }
            return bl;
        }
    }
}